//
//  HomeViewController.m
//  EastMarket
//
//  Created by 李鹏程 on 16/10/12.
//  Copyright © 2016年 guoguo. All rights reserved.
//

#import "HomeViewController.h"

@interface HomeViewController ()
{
    UIView *viewSlider;
    UIView *viewShopping;
    UIView *viewBottom;
    SZCirculationImageView *sliderImgView;
    UIImageView *imgLogo;
    UITabBarItem * item;
    UIView *viewNavigatinBtn;
    UILabel *lblTips;
}

@property (strong, nonatomic) NSMutableArray<CALayer *> *redLayers;

@end

@implementation HomeViewController
- (NSMutableArray<CALayer *> *)redLayers {
    if (!_redLayers) {
        _redLayers = [NSMutableArray array];
    }
    return _redLayers;
}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    imgLogo.hidden = false;
    _searchBar.hidden = false;
    _searchBar.text = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.title = @"首页";
    
    advBannerArr = [[NSMutableArray alloc] init];
    sliderAdvListArr = [[NSMutableArray alloc] init];
    recommendItemListArr = [[NSMutableArray alloc] init];
    specialRecommendItemListArr = [[NSMutableArray alloc] init];
    allDataSourceArr = [[NSMutableArray alloc] init];
   
    imgLogo = [[UIImageView alloc] initWithFrame:CGRectMake(8, 20 + 8, 28 * 1627 / 495, 28)];
    imgLogo.image = [UIImage imageNamed:@"logo"];
    [self.navigationController.view addSubview:imgLogo];
    
    // 搜索按钮
    _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(imgLogo.frame.origin.x + imgLogo.frame.size.width, 20, SCREEN_WIDTH - imgLogo.frame.origin.x - imgLogo.frame.size.width - 2 * 8,40)];
    _searchBar.delegate = self;
    _searchBar.backgroundColor = [UIColor clearColor];
    _searchBar.placeholder = @"输入关键字进行查找";
    [[[[_searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:0] removeFromSuperview];
    [_searchBar setSearchBarStyle:UISearchBarStyleDefault];
    
    _searchBar.tintColor = [UIColor blueColor];
    [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]
     
     setTitleTextAttributes:
     
     [NSDictionary dictionaryWithObjectsAndKeys:
      
      [UIColor whiteColor],
      
      UITextAttributeTextColor,
      
      [NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
      
      UITextAttributeTextShadowOffset,nil]
     
     forState:UIControlStateNormal];
    [self.navigationController.view addSubview:_searchBar];

    [self initHomePageUI];
    [self loadViewIndexPage];
    [SVProgressHUD showWithStatus:@"加载中"];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:EastMarket_MainToneColor];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    
    item = [self.tabBarController.tabBar.items objectAtIndex:3];
}

- (void)initHomePageUI {

    CGFloat spacing = 8.0f;
    
    UICollectionViewFlowLayout *layoutOfProMainCate = [UICollectionViewFlowLayout new];
    layoutOfProMainCate.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 44);
    
    CGFloat cellHeight = SCREEN_WIDTH / 2 - spacing * 2 + 26 * 3 + 3 * spacing + 2;
    
    layoutOfProMainCate.itemSize = CGSizeMake((self.view.frame.size.width - 2) / 2, cellHeight);//每个cell的大小
    layoutOfProMainCate.minimumLineSpacing = 2.0f;//每行的最小间距
    layoutOfProMainCate.minimumInteritemSpacing = 2.0f;//每列的最小间距
    layoutOfProMainCate.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);//网格视图的/上/左/下/右,的边距
    _collectionViewProCate = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT - TopBarHeight - TabBarHeight) collectionViewLayout:layoutOfProMainCate];
    [_collectionViewProCate registerClass:[HomeViewCell class] forCellWithReuseIdentifier:@"homeViewCell"];
    _collectionViewProCate.delegate = self;
    _collectionViewProCate.dataSource = self;
    _collectionViewProCate.backgroundColor = EastMarket_HomePageBackgroundColor;
    _collectionViewProCate.showsHorizontalScrollIndicator = false;
    _collectionViewProCate.showsVerticalScrollIndicator = false;
    [self.view addSubview:_collectionViewProCate];
    
    [_collectionViewProCate registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderViewTitle"];
     [_collectionViewProCate registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterViewTitle"];
    viewBottom = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - TopBarHeight - TabBarHeight, SCREEN_WIDTH, TabBarHeight)];
    viewBottom.backgroundColor = [UIColor clearColor];
    [self.view addSubview:viewBottom];
    [self.view bringSubviewToFront:viewBottom];
    
    viewShopping = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 3 / 5, SCREEN_HEIGHT - TopBarHeight - TabBarHeight, SCREEN_WIDTH / 5, TabBarHeight)];
    viewShopping.backgroundColor = [UIColor clearColor];
    [self.view addSubview:viewShopping];
}

// 请求首页数据接口
- (void)loadViewIndexPage {
    
    [NSThread detachNewThreadSelector:@selector(callServerGetIndexPage) toTarget:self  withObject:Nil];
}

- (void)callServerGetIndexPage {
    
    NSDictionary *dicIndexPage = [AppServiceInterface GetIndexPage];
    [self performSelectorOnMainThread:@selector(updateIndexPage:) withObject:dicIndexPage waitUntilDone:NO];
}

- (void)updateIndexPage:(NSDictionary *)dic {
    
    [SVProgressHUD dismiss];

    if (dic && [[dic objectForKey:@"success"] integerValue] == 1) {
        
        if ([[dic objectForKey:@"code"] isEqualToString:@"LoginRequire"]) {
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [self.navigationController pushViewController:loginVC animated:true];
            return;
        }
        [sliderAdvListArr removeAllObjects];
        [recommendItemListArr removeAllObjects];
        [specialRecommendItemListArr removeAllObjects];
        [allDataSourceArr removeAllObjects];
//        [advBannerArr addObject:[[dic objectForKey:@"data"] objectForKey:@"advBanner1"]];
//        [advBannerArr addObject:[[dic objectForKey:@"data"] objectForKey:@"advBanner2"]];
        
        advBannerOneDic = [[dic objectForKey:@"data"] objectForKey:@"advBanner1"];
        advBannerTwoDic = [[dic objectForKey:@"data"] objectForKey:@"advBanner2"];
        item.badgeValue = [NSString stringWithFormat:@"%@", [[dic objectForKey:@"data"] objectForKey:@"cartItemNum"]];
        
        [sliderAdvListArr addObjectsFromArray:[[dic objectForKey:@"data"] objectForKey:@"sliderAdvList"]];
        
        [recommendItemListArr addObjectsFromArray:[[dic objectForKey:@"data"] objectForKey:@"recommendItemList"]];
        [specialRecommendItemListArr addObjectsFromArray:[[dic objectForKey:@"data"] objectForKey:@"specialRecommendItemList"]];
        
        NSMutableDictionary *dictrecommendItem = [[NSMutableDictionary alloc] initWithCapacity:10];
        [dictrecommendItem setObject:recommendItemListArr forKey:@"data"];
        
        NSMutableDictionary *dictspecialRecommendItem = [[NSMutableDictionary alloc] initWithCapacity:10];
        [dictspecialRecommendItem setObject:specialRecommendItemListArr forKey:@"data"];
    
        [allDataSourceArr addObjectsFromArray:[NSArray arrayWithObjects:dictrecommendItem, dictspecialRecommendItem, nil]];
        [_collectionViewProCate reloadData];
    } else if(dic && [[dic objectForKey:@"success"] integerValue] != 1) {
    
        [GlobalUnit showAlertHint:@"温馨提示！" message:[NSString stringWithFormat:@"%@", [dic objectForKey:@"msg"]]];
    } else {
    
        [GlobalUnit showAlertHint:@"温馨提示！" message:@"请求服务器失败，请稍后重试"];
    }
}

- (void)btnGuideClicked:(UIButton *)btn {

    /*
    switch (btn.tag) {
        case 21:
            self.tabBarController.selectedIndex = 1;
            break;
        case 22:
            self.tabBarController.selectedIndex = 1;
            break;
        case 23:
            self.tabBarController.selectedIndex = 1;
            break;
        case 24:
            self.tabBarController.selectedIndex = 1;
            break;

        default:
            break;
    }
     */
    if (btn.tag == 21) {
        self.tabBarController.selectedIndex = 1;
//        ProductViewController *productVC = [[ProductViewController alloc] init];
//        [self.navigationController pushViewController:productVC animated:true];
    } else if (btn.tag == 22) {
    
        GetCouponCenterViewController *getCouponVC = [[GetCouponCenterViewController alloc] init];
        [getCouponVC setHidesBottomBarWhenPushed:true];
        [imgLogo setHidden:true];
        [_searchBar setHidden:true];
        [self.navigationController pushViewController:getCouponVC animated:true];
    } else if (btn.tag == 23) {
    
        SupplierViewController *supplierVC = [[SupplierViewController alloc] init];
        [supplierVC setHidesBottomBarWhenPushed:true];
        [imgLogo setHidden:true];
        [_searchBar setHidden:true];
        [self.navigationController pushViewController:supplierVC animated:true];
    } else if (btn.tag == 24) {
    
        MyOrderViewController *myOrderVC = [[MyOrderViewController alloc] init];
        [myOrderVC setHidesBottomBarWhenPushed:true];
        [imgLogo setHidden:true];
        [_searchBar setHidden:true];
        [self.navigationController pushViewController:myOrderVC animated:true];
    }
}

- (void)tapViewBuy:(UIGestureRecognizer *)tap {
    globalManager.strKeyName = nil;
    self.tabBarController.selectedIndex = 1;
}

- (void)tapViewCoupon:(UIGestureRecognizer *)tap {
    GetCouponCenterViewController *getCouponVC = [[GetCouponCenterViewController alloc] init];
    [getCouponVC setHidesBottomBarWhenPushed:true];
    [imgLogo setHidden:true];
    [_searchBar setHidden:true];
    [self.navigationController pushViewController:getCouponVC animated:true];
}

- (void)tapViewSupplier:(UIGestureRecognizer *)tap {
    SupplierViewController *supplierVC = [[SupplierViewController alloc] init];
    [supplierVC setHidesBottomBarWhenPushed:true];
    [imgLogo setHidden:true];
    [_searchBar setHidden:true];
    [self.navigationController pushViewController:supplierVC animated:true];
}

- (void)tapViewMyOrder:(UIGestureRecognizer *)tap {
    MyOrderViewController *myOrderVC = [[MyOrderViewController alloc] init];
    [myOrderVC setHidesBottomBarWhenPushed:true];
    [imgLogo setHidden:true];
    [_searchBar setHidden:true];
    [self.navigationController pushViewController:myOrderVC animated:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark --  UISearchBarDelegate & UISearchResultsUpdating delegate
// 取消按钮点击的回调
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    
    debugMethod();
    [_searchBar resignFirstResponder];
    _searchBar.showsCancelButton = false;
    _searchBar.text = nil;
}
// 搜索按钮点击的回调
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    
    debugMethod();
    [_searchBar resignFirstResponder];
    globalManager.strKeyName = _searchBar.text;
    self.tabBarController.selectedIndex = 1;
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    
    _searchBar.showsCancelButton = true;
    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    
    return YES;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

    debugMethod();
}


- (void)AOImageClick:(NSInteger)vid {

    self.tabBarController.selectedIndex = 1;
}

#pragma mark -- UICollectionView delegate * darasource
// 定义展示的UICollectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    NSDictionary *itemListDic = allDataSourceArr[section];
    NSArray * itemListDicArr = itemListDic[@"data"];
    return itemListDicArr.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return allDataSourceArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifyCate = @"homeViewCell";
    HomeViewCell *cellCate = [collectionView dequeueReusableCellWithReuseIdentifier:identifyCate forIndexPath:indexPath];
    cellCate.backgroundColor = [UIColor whiteColor];
    [cellCate sizeToFit];
    NSDictionary *CategoryNameDict = allDataSourceArr[indexPath.section];
    NSArray *itemListArr = CategoryNameDict[@"data"];
    [cellCate.iconImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [itemListArr[indexPath.row] objectForKey:@"itemPic"]]] placeholderImage:[UIImage imageNamed:@"NoProductImage"]];
    cellCate.lblPrice.text = [NSString stringWithFormat:@"￥%.2lf", [[itemListArr[indexPath.row] objectForKey:@"price"] floatValue]];
    cellCate.lblTitle.text = [NSString stringWithFormat:@"%@", [itemListArr[indexPath.row] objectForKey:@"title"]];
    cellCate.lblNum.text = [NSString stringWithFormat:@"%ld", [[itemListArr[indexPath.row] objectForKey:@"cartNum"] integerValue]];
    
    //找到每一个按钮；
    UIButton *btnAdd = cellCate.btnNumAdd;
    [btnAdd addTarget:self action:@selector(btnAddPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btnReduce = cellCate.btnNumReduce;
    [btnReduce addTarget:self action:@selector(btnReducePressed:) forControlEvents:UIControlEventTouchUpInside];
    
    /*
    cellCate.block=^(CGPoint point)
    {
        CGPoint startPoint = [self.view convertPoint:point fromView:_collectionViewProCate];
        CGPoint endpoint=[self.view convertPoint:viewShopping.center fromView:viewBottom];
        [self initCHLayerFromPoint:startPoint toPoint:endpoint];
    };
     */
    
    /*
    
    cellCate.btnNumReduceClick = ^() {
        debugMethod();
        debugLog(@"%ld", indexPath.row);
        if (cellCate.lblNum.text.integerValue > 0) {
            
            [self loadViewCartItem:[NSString stringWithFormat:@"%@", [itemListArr[indexPath.row] objectForKey:@"id"]] productNum:-1];
            [_collectionViewProCate reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
        } else {
            
            [GlobalUnit showAlertHint:@"温馨提示！" message:@"当前商品数目为0"];
        }
    };
    cellCate.btnNumAddClick = ^() {
        debugMethod();
        debugLog(@"%ld", indexPath.row);
        [self loadViewCartItem:[NSString stringWithFormat:@"%@", [itemListArr[indexPath.row] objectForKey:@"id"]] productNum:+1];
        [_collectionViewProCate reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
    };
     */

    return cellCate;
}

- (void)btnAddPressed:(UIButton *)sender {
    
    UIView *v = [sender superview];//获取父类view
    HomeViewCell *cell = (HomeViewCell *)[v superview];//获取cell
    
    NSIndexPath *indexPath = [_collectionViewProCate indexPathForCell:cell];//获取cell对应的indexpath;
    debugLog(@"%ld%ld", indexPath.section, indexPath.row);
    if (allDataSourceArr.count > 0) {
        NSDictionary *CategoryNameDict = allDataSourceArr[indexPath.section];
        NSArray *itemListArr = CategoryNameDict[@"data"];
        
        [self loadViewCartItem:[NSString stringWithFormat:@"%@", [itemListArr[indexPath.row] objectForKey:@"id"]] productNum:+1];
        [_collectionViewProCate reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
    }
}

- (void)btnReducePressed:(UIButton *)sender {

    UIView *v = [sender superview];//获取父类view
    HomeViewCell *cell = (HomeViewCell *)[v superview];//获取cell
    NSIndexPath *indexPath = [_collectionViewProCate indexPathForCell:cell];//获取cell对应的indexpath;
    debugLog(@"点击了第%ld组，第%ld个item", indexPath.section, indexPath.row);
    if (allDataSourceArr.count > 0) {
        NSDictionary *CategoryNameDict = allDataSourceArr[indexPath.section];
        NSArray *itemListArr = CategoryNameDict[@"data"];
        [self loadViewCartItem:[NSString stringWithFormat:@"%@", [itemListArr[indexPath.row] objectForKey:@"id"]] productNum:-1];
        [_collectionViewProCate reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
    }
}

// 添加商品到购物车
- (void)loadViewCartItem:(NSString *)productID productNum:(NSInteger)productNums {
    
    _strProductID = productID;
    _productNumber = productNums;
    [NSThread detachNewThreadSelector:@selector(callServerGetPCartItem) toTarget:self  withObject:Nil];
}

- (void)callServerGetPCartItem {
    
    NSDictionary *dict = [AppServiceInterface cartItem:_strProductID itemNum:_productNumber];
    [self performSelectorOnMainThread:@selector(updateCartItem:) withObject:dict waitUntilDone:NO];
}

- (void)updateCartItem:(NSDictionary *)dic {
    
    if (dic && [[dic objectForKey:@"success"] integerValue] == 1) {
        if (![[dic objectForKey:@"data"] isKindOfClass:[NSNull class]]) {
            [NSThread detachNewThreadSelector:@selector(callServerGetIndexPage) toTarget:self  withObject:Nil];
        }
    }
}

- (void)initCHLayerFromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint {
    
    CALayer *chLayer = [[CALayer alloc] init];
    [self.redLayers addObject:chLayer];
    chLayer.frame = CGRectMake(startPoint.x, startPoint.y, 15, 15);
    chLayer.cornerRadius = CGRectGetWidth(chLayer.frame)/2.f;
    chLayer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:chLayer];
    
    CAKeyframeAnimation *CHAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y);
    CGPathAddQuadCurveToPoint(path, NULL, endPoint.x, startPoint.y, endPoint.x, endPoint.y);
    
    CHAnimation.path = path;
    CHAnimation.removedOnCompletion = NO;
    CHAnimation.fillMode = kCAFillModeBoth;
    CHAnimation.duration = 0.5;
    CHAnimation.delegate = self;
    [chLayer addAnimation:CHAnimation forKey:nil];
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    [self.redLayers[0] removeFromSuperlayer];
    [self.redLayers removeObjectAtIndex:0];
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
//    debugMethod();
//    debugLog(@"***************%ld", indexPath.row);
}

- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader)
    {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderViewTitle" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            CGFloat spacing = 8.0f;
            CGFloat border = 16.0f;
            CGFloat iconHeight = 54.0f;
            
            UIView *headerViewBack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (SCREEN_HEIGHT - TopBarHeight) / 3 + SCREEN_HEIGHT / 6 + 32 + 8 + 8)];
            headerViewBack.backgroundColor = [UIColor clearColor];
            [headerView addSubview:headerViewBack];
            
            viewSlider = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (SCREEN_HEIGHT - TopBarHeight) / 3)];
            viewSlider.backgroundColor = EastMarket_darkGreyFontColor;
            [headerViewBack addSubview:viewSlider];
            
            NSMutableArray *pathArray = [[NSMutableArray alloc] initWithCapacity:10];
            for (NSDictionary *dictSlider in sliderAdvListArr) {
                [pathArray addObject:[dictSlider objectForKey:@"imgSrc"]];
            }
            
            sliderImgView = [[SZCirculationImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (SCREEN_HEIGHT - TopBarHeight) / 3) andImageURLsArray:pathArray];
            sliderImgView.pauseTime = 1.0;
            sliderImgView.vDelegate = self;
            [viewSlider addSubview:sliderImgView];
            sliderImgView.hidden = NO;
            viewSlider.hidden = NO;
            
            viewNavigatinBtn = [[UIView alloc] initWithFrame:CGRectMake(0, viewSlider.frame.origin.y + viewSlider.frame.size.height + spacing, SCREEN_WIDTH, SCREEN_HEIGHT / 6)];
            viewNavigatinBtn.backgroundColor = [UIColor whiteColor];
            
            [headerViewBack addSubview:viewNavigatinBtn];
            
            NSArray *btnTitleArr = @[@"开始购买", @"领劵中心", @"供应商", @"我的订单"];
            NSArray *imgGuideArr = @[@"buy", @"coupon", @"supplier", @"myorder"];
            for (int i = 0; i < btnTitleArr.count; i++) {
                imgGuide = [[UIImageView alloc] initWithFrame:CGRectMake(border + (viewNavigatinBtn.frame.size.width - 2 * border - 4 * iconHeight) / 3 * i + i * iconHeight,border, iconHeight, iconHeight)];
                imgGuide.backgroundColor = [UIColor clearColor];
                imgGuide.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", imgGuideArr[i]]];
                imgGuide.tag = 999;
                imgGuide.userInteractionEnabled = true;
                if (i == 0) {
                    UITapGestureRecognizer *tapViewBuy = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewBuy:)];
                    [imgGuide addGestureRecognizer:tapViewBuy];
                } else if (i == 1) {
                    
                    UITapGestureRecognizer *tapViewCoupon = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewCoupon:)];
                    [imgGuide addGestureRecognizer:tapViewCoupon];
                } else if (i == 2) {
                    
                    UITapGestureRecognizer *tapViewSupplier = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewSupplier:)];
                    [imgGuide addGestureRecognizer:tapViewSupplier];
                } else if (i == 3) {
                    
                    UITapGestureRecognizer *tapViewMyOrder = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewMyOrder:)];
                    [imgGuide addGestureRecognizer:tapViewMyOrder];
                }
        
                
                btnGuide = [[UIButton alloc] initWithFrame:CGRectMake(imgGuide.frame.origin.x,imgGuide.frame.origin.y + imgGuide.frame.size.height + spacing, imgGuide.frame.size.width, 22)];
                [btnGuide setTitle:btnTitleArr[i] forState:UIControlStateNormal];
                [btnGuide setTitleColor:EastMarket_darkGreyFontColor forState:UIControlStateNormal];
                btnGuide.titleLabel.font = [UIFont systemFontOfSize:12];
                btnGuide.tag = 21 + i;
                [btnGuide addTarget:self action:@selector(btnGuideClicked:) forControlEvents:UIControlEventTouchUpInside];
                [viewNavigatinBtn addSubview:btnGuide];
                [viewNavigatinBtn addSubview:imgGuide];
            }
            viewNavigatinBtn.hidden = NO;
            imgGuide.hidden = NO;
            btnGuide.hidden = NO;
            lblTips = [[UILabel alloc] initWithFrame:CGRectMake(0, viewNavigatinBtn.frame.origin.y + viewNavigatinBtn.frame.size.height + spacing, headerView.frame.size.width, 32)];
            lblTips.font = [UIFont systemFontOfSize:16];
            lblTips.text = @"  天天特价";
            lblTips.backgroundColor = [UIColor whiteColor];
//            viewNavigatinBtn.hidden = NO;
            [headerViewBack addSubview:lblTips];
        }
        else if(indexPath.section == 1){
            sliderImgView.hidden = YES;
            viewSlider.hidden = YES;
            viewNavigatinBtn.hidden = YES;
            imgGuide.hidden = YES;
            btnGuide.hidden = YES;
            
//            lblTips = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height)];
            lblTips.frame = CGRectMake(lblTips.frame.origin.x, 0, lblTips.frame.size.width, headerView.frame.size.height);
            lblTips.font = [UIFont systemFontOfSize:16];
            lblTips.text = @"  新品推荐";
            lblTips.backgroundColor = [UIColor whiteColor];
            [headerView addSubview:lblTips];
        }
        reusableview = headerView;
    }
        if (kind == UICollectionElementKindSectionFooter)
        {
            UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterViewTitle" forIndexPath:indexPath];
            UIImageView *bannerImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 8, footerView.frame.size.width, footerView.frame.size.height - 16)];
            bannerImg.userInteractionEnabled = true;
            
            if (indexPath.section == 0) {
                [bannerImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [advBannerOneDic objectForKey:@"imgSrc"]]] placeholderImage:[UIImage imageNamed:@"NoProductImage"]];
                UITapGestureRecognizer *tapViewSnappedUp = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SnappedUp:)];
                [bannerImg addGestureRecognizer:tapViewSnappedUp];
                
            } else if (indexPath.section == 1) {
                [bannerImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [advBannerTwoDic objectForKey:@"imgSrc"]]] placeholderImage:[UIImage imageNamed:@"NoProductImage"]];
                UITapGestureRecognizer *tapViewSnappedUp = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SnappedUp:)];
                [bannerImg addGestureRecognizer:tapViewSnappedUp];
            }
            [footerView addSubview:bannerImg];
            reusableview = footerView;
        }
    return reusableview;
}

- (void)SnappedUp:(UIGestureRecognizer *)tap {

    self.tabBarController.selectedIndex = 1;
    globalManager.strKeyName = nil;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return CGSizeMake(SCREEN_WIDTH, (SCREEN_HEIGHT - TopBarHeight) / 3 + SCREEN_HEIGHT / 6 + 32 + 8 + 9);
    } else {
        return CGSizeMake(SCREEN_WIDTH, 32);
    }
    // 750 x 180
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {

    return CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH * 180 / 750);
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
